import { createRouter, createWebHistory } from 'vue-router'
import {ElMessage} from 'element-plus'
const routes = [
    {
      path: '/',
      name: 'main',
      component: () => import('@/views/Main.vue'),
      redirect: '/login/form',
      children:[
        {
          path: 'home',
          name: 'home',
          component: () => import('@/views/Home.vue')
        },
        {
          path: 'allFiles',
          name: 'allFiles',
          component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'folder',
          name: 'folder',
          component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'image',
          name: 'image',
          component: () => import('@/components/main/CommonMain.vue')
        },
        
        {
          path: 'video',
          name: 'video',
           component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'document',
          name: 'document',
           component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'audio',
          name: 'audio',
           component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'other',
          name: 'other',
           component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'recycle',
          name: 'recycle',
           component: () => import('@/components/main/CommonMain.vue')
        },
        {
          path: 'chat',
          name: 'chat',
           component: () => import('@/views/Chat.vue')
        },
        {
          path: 'user',
          name: 'user',
           component: () => import('@/views/User.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login.vue'),
       redirect: '/login/form',
       children:[
        {
          path: 'form',//默认路由
          name: 'LoginForm',
          component: () => import('@/components/Login/LoginForm.vue')
        },
        {
          path: 'register',//子路由前缀不带/
          name: 'RegisterForm',
          component: () => import('@/components/Login/RegisterForm.vue')
        }
      ]
    },
    
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('token')
  
  // 定义需要登录的路由白名单
  // const authPages = ['/login/form', '/login/register']
  const needAuthPaths = ['/','/home', '/allFiles', '/folder', '/image', '/video', '/document', '/audio', '/other', '/recycle', '/chat', '/user']
  // 如果用户已登录但尝试访问登录/注册页
  if (isLoggedIn && to.path.startsWith('/login')) {
    ElMessage.warning('您已登录，将跳转到首页')
    return next('/home')
  }

  // 如果用户未登录且访问需要认证的页面
  if (!isLoggedIn && needAuthPaths.includes(to.path)) {
    ElMessage.error('请先登录')
    return next('/login/form') // 明确跳转到登录表单页
  }

  next()
})
export default router   